#include "copymakeborder_plugin.h"

#include "ui_plugin.h"

CopyMakeBorder_Plugin::CopyMakeBorder_Plugin()
{
    // Insert initialization codes here ...
}

CopyMakeBorder_Plugin::~CopyMakeBorder_Plugin()
{
    // Insert cleanup codes here ...
}

QString CopyMakeBorder_Plugin::title()
{
    return this->metaObject()->className();
}

QString CopyMakeBorder_Plugin::version()
{
    return "1.0.0";
}

QString CopyMakeBorder_Plugin::description()
{
    return "";
}

QString CopyMakeBorder_Plugin::help()
{
    return "";
}

void CopyMakeBorder_Plugin::setupUi(QWidget *parent)
{
    ui = new Ui::PluginGui;
    ui->setupUi(parent);
    QStringList items;
    items.append("BORDER_CONSTANT");
    items.append("BORDER_REPLICATE");
    items.append("BORDER_REFLECT");
    items.append("BORDER_WRAP");
    items.append("BORDER_REFLECT_101");
    ui->borderTypeComboBox->addItems(items);
    ui->borderTypeComboBox->setCurrentIndex(-1);
    connect(ui->borderTypeComboBox,
            SIGNAL(currentIndexChanged(int)),
            this,
            SLOT(on_borderTypeComboBox_currentIndexChanged(int)));

    connect(ui->topSpinBox,
            SIGNAL(valueChanged(int)),
            this,
            SLOT(on_topSpinBox_valueChanged(int)));
    connect(ui->botSpinBox,
            SIGNAL(valueChanged(int)),
            this,
            SLOT(on_botSpinBox_valueChanged(int)));
    connect(ui->leftSpinBox,
            SIGNAL(valueChanged(int)),
            this,
            SLOT(on_leftSpinBox_valueChanged(int)));
    connect(ui->rightSpinBox,
            SIGNAL(valueChanged(int)),
            this,
            SLOT(on_rightSpinBox_valueChanged(int)));

    connect(ui->rSpinBox,
            SIGNAL(valueChanged(int)),
            this,
            SLOT(on_rSpinBox_valueChanged(int)));
    connect(ui->gSpinBox,
            SIGNAL(valueChanged(int)),
            this,
            SLOT(on_gSpinBox_valueChanged(int)));
    connect(ui->bSpinBox,
            SIGNAL(valueChanged(int)),
            this,
            SLOT(on_bSpinBox_valueChanged(int)));

    connect(ui->urlLabel,SIGNAL(linkActivated(QString)),this,SLOT(openURL(QString)));
}

void CopyMakeBorder_Plugin::processImage(const cv::Mat &inputImage, cv::Mat &outputImage)
{
    using namespace cv;
    int top, bot, left, right;
    top = ui->topSpinBox->value();
    bot = ui->botSpinBox->value();
    left = ui->leftSpinBox->value();
    right = ui->rightSpinBox->value();

    Scalar value=Scalar(ui->bSpinBox->value(),
                        ui->gSpinBox->value(),
                        ui->rSpinBox->value());

    QString s = "BORDER_CONSTANT";
    if(ui->borderTypeComboBox->currentText()==s)
    {
        ui->bSpinBox->setEnabled(true);
        ui->gSpinBox->setEnabled(true);
        ui->rSpinBox->setEnabled(true);
    }
    else {
        ui->bSpinBox->setEnabled(false);
        ui->gSpinBox->setEnabled(false);
        ui->rSpinBox->setEnabled(false);
    }
    copyMakeBorder(inputImage,
                   outputImage,
                   top,
                   bot,
                   left,
                   right,
                   ui->borderTypeComboBox->currentIndex(),
                   value);
}

void CopyMakeBorder_Plugin::openURL(QString url)
{
    QDesktopServices::openUrl(url);
}

void CopyMakeBorder_Plugin::on_borderTypeComboBox_currentIndexChanged(int index)
{
    Q_UNUSED(index);
    ui->topSpinBox->setEnabled(true);
    ui->botSpinBox->setEnabled(true);
    ui->leftSpinBox->setEnabled(true);
    ui->rightSpinBox->setEnabled(true);
    emit updateNeeded();
}

void CopyMakeBorder_Plugin::on_topSpinBox_valueChanged(int arg1)
{
    Q_UNUSED(arg1);
    emit updateNeeded();
}

void CopyMakeBorder_Plugin::on_botSpinBox_valueChanged(int arg1)
{
    Q_UNUSED(arg1);
    emit updateNeeded();
}

void CopyMakeBorder_Plugin::on_leftSpinBox_valueChanged(int arg1)
{
    Q_UNUSED(arg1);
    emit updateNeeded();
}

void CopyMakeBorder_Plugin::on_rightSpinBox_valueChanged(int arg1)
{
    Q_UNUSED(arg1);
    emit updateNeeded();
}


void CopyMakeBorder_Plugin::on_rSpinBox_valueChanged(int arg1)
{
    Q_UNUSED(arg1);
    emit updateNeeded();
}

void CopyMakeBorder_Plugin::on_gSpinBox_valueChanged(int arg1)
{
    Q_UNUSED(arg1);
    emit updateNeeded();
}

void CopyMakeBorder_Plugin::on_bSpinBox_valueChanged(int arg1)
{
    Q_UNUSED(arg1);
    emit updateNeeded();
}
